package com.weitao.marketserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.weitao.marketserver.config.AliPayConfig;
import com.weitao.marketserver.utils.FileTypeTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileCon {
    public Logger logger = LoggerFactory.getLogger(FileCon.class);


    @Value("${user.file.path}")
    public String filePath;

    @RequestMapping("upload")
    public String upload(@RequestParam Map<String,Object> param) {

        MultipartFile file= (MultipartFile) param.get("file");

        try {
            if (file.isEmpty()) {
                return "empty";
            }

            String type = "not get";
            type = FileTypeTest.getFileType(file);
            logger.info(type);

            if (type.equals("jpg") || type.equals("png") || type.equals("gif") || type.equals("jpeg")) {
                String fileName = System.currentTimeMillis() + "." + type;
                String path = filePath + fileName;

                File dest = new File(path);

                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }

                file.transferTo(dest);
                return fileName;
            } else {
                return "file type bad";
            }


        } catch (Exception e) {
            e.printStackTrace();

            return "faild";
        }


    }


    @PostMapping("/test")
    public String test(@RequestParam Map<String,Object> param,@RequestParam MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println(param);

        String path = filePath + System.currentTimeMillis()+"fileName.jpg";
        File dest = new File(path);
        file.transferTo(dest);

        return "ok";
    }

    @RequestMapping("/pay/{id}")
    public String paytest(@PathVariable(name = "id") int id) throws Exception {


        logger.info("idis:"+id+StpUtil.getLoginId());;
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.URL, AliPayConfig.APPID,AliPayConfig.RSA_PRIVATE_KEY,"json",
                AliPayConfig.CHARSET,AliPayConfig.ALIPAY_PUBLIC_KEY,"RSA2");
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setNotifyUrl("");
        request.setReturnUrl("http://www.baidu.com");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "20210817010101004");
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "测试商品");
        bizContent.put("product_code", "QUICK_WAP_WAY");

        request.setBizContent(bizContent.toString());
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);

        if (response.isSuccess()){
            return  response.getBody();
        }

        return "11";
    }

    @PostMapping("/post")
    public String posttest(@RequestParam Map<String,Object> param) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        param.put("bbb","ccc");
        param.put("bbb2","ccc");

        logger.info(objectMapper.writeValueAsString(param));

        String path = filePath + System.currentTimeMillis()+"fileName.jpg";

        File dest = new File(path);
        MultipartFile multipartFile= (MultipartFile) param.get("file");
        multipartFile.transferTo(dest);


        return "aa";
    }


}
